package utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则校验工具类
 * @author ASUS
 *
 */
public class RegularCheckUtils {

	/**
	 * 身份证
	 */
	public static final String REGEX_ID_CARD = "^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X|x)$";
	/**
	 * 港澳台通行证
	 */
	public static final String REGEX_ID_HKMT = "/([A-Z]{1,2}[0-9]{6}([0-9A]))|(^[1|5|7][0-9]{6}\\([0-9Aa]\\))|([A-Z][0-9]{9})/";
	/**
	 * 护照
	 */
	public static final String REGEX_ID_PASSPORT = " /^1[45][0-9]{7}|([P|p|S|s]\\d{7})|([S|s|G|g]\\d{8})|([Gg|Tt|Ss|Ll|Qq|Dd|Aa|Ff]\\d{8})|([H|h|M|m]\\d{8,10})$/";
	/**
	 * 军官证
	 */
	public static final String REGEX_ID_COO = " /[\\u4e00-\\u9fa5](字第){1}(\\d{4,8})(号?)$/";
	/**
	 * 台胞证
	 */
	public static final String REGEX_ID_TB = "^[A-Z](\\d{9})$";



   /**
    * 匹配是否符合正则表达式pattern 匹配返回true
    * @param str 匹配的字符串
    * @param pattern 匹配模式
    * @return boolean
    */
    private static boolean regular(String str,String pattern){
	    if(null == str || str.trim().length() <= 0) {
	    	return false;
	    }
	    Pattern p = Pattern.compile(pattern);
	    Matcher m = p.matcher(str);
	    return m.matches();
    }

    /**
     * 身份证 15或18位
     */
    public static boolean isIDCardNo(String str) {
    	if(StringUtils.isEmpty(str))
    		return false;
    	if(str.trim().length() == 15 || str.trim().length() == 18) {
    		return regular(str, REGEX_ID_CARD);
        } else {
        	return false;
        }
    }

    /**
     * 验证护照
     */
    public static boolean isPassport(String str) {
    	return regular(str, REGEX_ID_PASSPORT);
    }

    /**
     * 验证港澳台通行证
     */
    public static boolean isHKMT(String str) {
    	return regular(str, REGEX_ID_HKMT);
    }

    /**
     * 验证台胞证
     */
    public static boolean isTBCard(String str) {
    	return regular(str, REGEX_ID_TB);
    }

    public static void main(String[] args) {
    	if(isTBCard("A123456719")){
    		System.out.println("==========合法==============");
    	}else{
    		System.out.println("==========非法==============");
    	}
	}

}
